Method: HexaPDF::PDFArray#[]

Defined in:
lib/hexapdf/pdf_array.rb

#[](arg1, arg2 = nil) ⇒ Object

:call-seq:

array[index]             -> obj or nil
array[start, length]     -> new_array or nil
array[range]             -> new_array or nil

Returns the value at the given index, or a subarray using the given start and length, or a subarray specified by range.

This method should be used instead of direct access to a value because it provides some advantages:

  • References are automatically resolved.

  • Returns the native Ruby object for values with class HexaPDF::Object. However, all subclasses of HexaPDF::Object are returned as is (it makes no sense, for example, to return the hash that describes the Catalog instead of the Catalog object).

Note: Hash or Array values will always be returned as-is, i.e. not wrapped with Dictionary or PDFArray.


71
72
73
74
75
76
77
78
79
80
81
# File 'lib/hexapdf/pdf_array.rb', line 71

def [](arg1, arg2 = nil)
  data = arg2 ? value[arg1, arg2] : value[arg1]
  return if data.nil?

  if arg2 || arg1.kind_of?(Range)
    index = (arg2 ? arg1 : arg1.begin)
    data.map! {|item| process_entry(item, index).tap { index += 1 } }
  else
    process_entry(data, arg1)
  end
end